<template>
  <div class="app-container">
    <div class="contentBox">
      <div class="searchBox">
        <el-form :model="queryParams" ref="queryRef" :inline="true" @submit.prevent>
          <el-form-item prop="name" style="margin: 0 6px 0 0;">
            <el-input
                class="inputBox"
                v-model="queryParams.name"
                placeholder="请输入名称"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item style="margin: 0 6px 0 0;">
            <el-button class="normalFormBg searchBtn" icon="Search" @click="handleQuery"></el-button>
            <el-button class="normalFormBg searchBtn" icon="Refresh" @click="resetQuery"></el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-row :gutter="10" style="margin: 16px 0;">
        <el-col :span="1.5">
          <div class="normalBtn pageFlex pageFlexCenter" v-hasPermi="['gasManage:defaultSetting:userInfo:add']"
            @click="handleModalShow('新增用户')">
            <img src="@/assets/images/plus.png" class="normalBtn_icon">
            <div class="normalBtn_text">新增</div>
          </div>
        </el-col>
      </el-row>
      <el-table
          v-loading="loading"
          :data="dataList"
          border>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="date" label="用户编号" width="150" :show-overflow-tooltip="true" fixed="left"></el-table-column>
        <el-table-column prop="creditCode" label="用户名称" width="200"></el-table-column>
        <el-table-column prop="creditCode" label="手机号码" width="150"></el-table-column>
        <el-table-column prop="creditCode" label="门牌号" width="200"></el-table-column>
        <el-table-column prop="creditCode" label="通信地址" width="250" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" width="340" align="center" fixed="right">
          <template #default="scope">
            <div class="pageFlex pageFlexCenter" style="width: 100%;">
              <div class="pageFlex pageFlexCenter btnSetBox" v-hasPermi="['gasManage:defaultSetting:userInfo:list']"
                   @click="handleInfo(scope.row.id)">
                <img src="@/assets/images/fileManage.png" class="img">
                <div class="text">详情</div>
              </div>
              <div class="pageFlex pageFlexCenter btnSetBox" v-hasPermi="['gasManage:defaultSetting:userInfo:edit']"
                   @click="handleEdit(scope.row.id)">
                <img src="@/assets/images/edit.png" class="img">
                <div class="text">修改</div>
              </div>
              <div class="pageFlex pageFlexCenter btnSetBox" v-hasPermi="['gasManage:defaultSetting:userInfo:delete']"
                   @click="handleDelete(scope.row)">
                <img src="@/assets/images/delete.png" class="img">
                <div class="text">删除</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="暂无数据" style="color: var(--el-color-primary);"/>
        </template>
      </el-table>
      <div class="pageFlex pageAlignCenter pageSpaceBetween" style="margin-top: 20px;">
        <div class="totalText">共计 {{total}} 条</div>
        <el-pagination background layout="prev, pager, next" :total="total" :current-page="queryParams.pageNum" :page-size="queryParams.pageSize" @current-change="handleCurrentChange"/>
      </div>
    </div>
    <el-drawer v-model="modalShow" direction="rtl" size="600" :title="modalTitle">
      <div class="modalBox">
        <div class="modalBoxContent">
          <el-form
              ref="formRef"
              class="formContent"
              label-width="100"
              :model="form"
              :rules="rules"
              :disabled="modalTitle === '用户详情'"
          >
            <div class="formContentTop pageFlex pageAlignCenter">
              <div class="line"></div>
              <div class="title">用户基本信息</div>
            </div>
            <div class="formContentCenter bgFF">
              <el-form-item label="用户名" prop="userName">
                <el-input
                  class="inputBox"
                  v-model="form.userName"
                  placeholder="请输入用户名"
                  clearable
                />
              </el-form-item>
              <el-form-item label="手机号" prop="phoneNumber">
                <el-input
                  class="inputBox"
                  v-model="form.phoneNumber"
                  placeholder="请输入手机号"
                  clearable
                />
              </el-form-item>
              <el-form-item label="用户密码"></el-form-item>
              <el-form-item label="证件类型"></el-form-item>
              <el-form-item label="证件号码"></el-form-item>
              <el-form-item label="邮箱"></el-form-item>
              <el-form-item label="状态" prop="isCancel">
                <el-radio-group v-model="form.isCancel">
                  <el-radio
                      v-for="dict in gas_meter_user_status"
                      :key="dict.value"
                      :value="dict.value"
                  >{{ dict.label }}</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="销户原因" prop="cancelReason" v-if="form.isCancel === '1'">
                <el-input
                  type="textarea"
                  class="textareaBox"
                  v-model="form.cancelReason"
                  placeholder="请输入销户原因"
                  :rows="6"
                  clearable
                />
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div class="lineBox" v-if="modalTitle !== '用户详情'"></div>
        <div class="bottomBox pageFlex pageFlexCenter" v-if="modalTitle !== '用户详情'">
          <el-button type="primary" :loading="submitLoading" @click="submitNow">确认提交</el-button>
        </div>
        <el-button type="primary" :loading="formLoading" class="modalLoadingBox" v-if="formLoading">加载数据中</el-button>
      </div>
    </el-drawer>
  </div>
</template>
<script setup>
import { onMounted } from 'vue';
import { searchUserInfoApi, addUserInfoApi, searchUserInfoByIdApi, editUserInfoApi, deleteUserInfoApi } from '@/api/gasManage/defaultSetting/userInfo';
import {ElLoading, ElMessageBox} from 'element-plus';
const { proxy } = getCurrentInstance();
const { gas_meter_user_status } = proxy.useDict("gas_meter_user_status");
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  name: ''
})
const loading = ref(false);
const total = ref(0);
const dataList = ref([]);
function handleQuery(){
  queryParams.value.pageNum = 1;
  getList();
}
function resetQuery(){
  proxy.resetForm("queryRef");
  handleQuery();
}
async function getList(){
  loading.value = true;
  let data = await getUserInfoList(queryParams.value);
  if(data && Array.isArray(data)){
    dataList.value = data;
  }else{
    dataList.value = [];
  }
  loading.value = false;
}
function getUserInfoList(params){
  return new Promise(resolve => {
    searchUserInfoApi(params).then(res => {
      total.value = Number(res.total);
      resolve(Array.isArray(res.rows) ? res.rows : []);
    }).catch(() => {resolve(false)})
  })
}
function handleCurrentChange(e){
  queryParams.value.pageNum = e;
  getList();
}
// ----------------------------弹窗-------------------------------
const modalShow = ref(false);
const modalTitle = ref('');
const formLoading = ref(false);
const submitLoading = ref(false);
const form = ref({
  userName: '',
  phoneNumber: '',
  isCancel: '0',
  cancelReason: ''
})
const rules = ref({
  userName: [{ required: true, message: "用户名称不能为空", trigger: "blur" }],
  phoneNumber: [
    { required: true, message: "手机号不能为空", trigger: "blur" },
    {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,message: "请输入正确的手机号",trigger: "blur"}
  ],
  cancelReason: [{ required: true, message: "销户原因不能为空", trigger: "blur" }]
});
function handleModalShow(name){
  proxy.resetForm('formRef');
  modalTitle.value = name;
  modalShow.value = true;
}
function handleModalHide(){
  modalShow.value = false;
}
function submitNow(){
  if(submitLoading.value) return;
  proxy.$refs["formRef"].validate(valid => {
    if(valid){
      submitLoading.value = true;
      if(modalTitle.value === '新增用户'){
        addNow();
      }else{
        editNow();
      }
    }else{
      proxy.$modal.alertWarning('信息填写有误，请检查！');
    }
  })
}
// ----------------------------详情-------------------------------
function handleInfo(id){
  // handleModalShow('品牌详情');
  // setFormData(id);
}
// ----------------------------修改-------------------------------
function handleEdit(id){
  // handleModalShow('修改品牌信息');
  // setFormData(id);
}
function editNow(){
  console.log(form.value)
  // editBrandApi(form.value).then(() => {
  //   submitLoading.value = false;
  //   handleModalHide();
  //   proxy.$modal.msgSuccess('修改品牌信息成功');
  //   getList();
  // }).catch(()=>{
  //   submitLoading.value = false;
  // })
}
// ----------------------------新增-------------------------------
function addNow(){
  console.log(form.value)
  // addBrandApi(form.value).then(() => {
  //   submitLoading.value = false;
  //   handleModalHide();
  //   proxy.$modal.msgSuccess('新增品牌成功');
  //   getList();
  // }).catch(()=>{
  //   submitLoading.value = false;
  // })
}
//----------------------删除--------------------------
function handleDelete(row){
  ElMessageBox.confirm(`确认删除用户名称为【${row.name}】的数据`,'提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    deleteNow(row);
  }).catch(() => {})
}
async function deleteNow(row){
  const loading = ElLoading.service({
    lock: true,
    text: '删除中~',
    background: 'rgba(0, 0, 0, 0.7)',
  })
  deleteUserInfoApi(row.id).then(()=>{
    loading.close();
    proxy.$modal.msgSuccess(row.name+'用户删除成功');
    getList();
  }).catch(()=>{
    loading.close();
  })
}
onMounted(()=>{
  getList();
})
</script>
<style lang='scss' scoped>
@import "@/assets/styles/public.scss";
:deep(.el-input){
  .el-input__wrapper{
    background-color: $formBgSet;
    box-shadow: none;
  }
}
.app-container{
  width: 100%;
  min-height: calc(100vh - 66px);
  background: #F8F8F9;
  .contentBox{
    width: 100%;
    min-height: $pageMinHeight;
    background-color: #FFFFFF;
    border-radius: 5px;
    box-sizing: border-box;
    padding: 12px;
    .searchBtn{
      width: 32px;
      height: 32px;
      font-size: 12px;
      padding: 0;
      border: none;
      margin: 0 6px 0 0px;
    }
    .btnSetBox{
      padding: 0 10px;
      box-sizing: border-box;
      border-right: 1px solid $borderColor;
      cursor: pointer;
      .text{
        height: 16px;
        line-height: 16px;
        color: var(--el-color-primary);
      }
      .img{
        width: 16px;
        height: 16px;
        margin-right: 2px;
      }
      &:last-child{
        border-right: none;
      }
    }
    .totalText{
      height: 22px;
      font-weight: 400;
      font-size: 14px;
      color: #1D2129;
      line-height: 22px;
    }
  }
}
:deep(.el-drawer){
  .el-drawer__header{
    margin: 0;
    padding-bottom: 20px;
  }
  .el-drawer__body{
    padding: 0;
  }
}
</style>

